﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class Business_Cisoft3G_InboxManage_FeedbackManage : WebUI.Page.BasePage
{
    //类型、姓名
    Cisoft.Cisoft3G.BLL.ContactsBO bllContactsShow = new Cisoft.Cisoft3G.BLL.ContactsBO();
    Cisoft.Cisoft3G.BLL.SmsTypeBO bllSmsTypeShow = new Cisoft.Cisoft3G.BLL.SmsTypeBO();

    //定义页数
    private static int pageNum = 0;
    private DataTable dt;

    #region 页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ltScript.Text = "<script type=\"text/javascript\" src=\"../../../js/Basic/BasicPage.js\"></script><script type=\"text/javascript\" src=\"../../../js/My97DatePicker/WdatePicker.js\"></script>";
            chcAll.Attributes.Add("onclick", "SelectAllCheckboxes(this)");
            imgStartDate.Attributes.Add("onclick", "WdatePicker({el:'txtStartTime'})");
            imgEndDate.Attributes.Add("onclick", "WdatePicker({el:'txtEndTime'})");
            Bind();
        }
    }
    #endregion

    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void Bind()
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        dt = new Cisoft.Cisoft3G.BLL.InboxBO().GetListWithPage(cpn, 13, FillString()).Tables[0];
        pageNum = dt.Rows.Count;
        if (dt.Rows.Count > 0)
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.InboxBO().GetListCount(cpn, 13, FillString());
            AspNetPager1.PageSize = 13;
        }
        else
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.InboxBO().GetListCount(cpn, 13, FillString());
        }
    }
    #endregion

    #region 翻页事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 删除事件
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<int> list = new List<int>();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (((CheckBox)item.FindControl("cbk_single")).Checked)
            {
                Label lblInboxID = item.FindControl("lblInboxID") as Label;
                if (!string.IsNullOrEmpty(lblInboxID.Text))
                {
                    list.Add(int.Parse(lblInboxID.Text));
                }
            }
        }

        Cisoft.Cisoft3G.BLL.InboxBO inbo = new Cisoft.Cisoft3G.BLL.InboxBO();
        string strError = string.Empty;
        if (list.Count > 0)
        {
            try
            {
                foreach (int id in list)
                {
                    inbo.Delete(id);
                }
            }
            catch (Exception ex)
            {
                strError = ex.Message;
            }
            if (string.IsNullOrEmpty(strError))
            {
                btnCancel.Visible = false;
                //PageRight("删除成功！", "FeedbackManage.aspx", "返回 ", "");
                PageRight(ModalPopupExtender1, lblTS, btnConfirm2, "<h4>删除成功！</h4>");
            }
            else
            {
                btnCancel.Visible = false;
                //PageError("删除失败，原因如下：" + strError + "。", Request.RawUrl);
                PageError(ModalPopupExtender1, lblTS, btnConfirm2, "删除失败，原因如下：" + strError);
            }
            Bind();
        }
    }
    #endregion

    #region 获取查询条件
    private string AppendString(string mobile, string content, string starttime, string endtime)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(" 1=1 ");
        if (ddlSearchCondition.SelectedIndex == 0)
        {
            sb.Append(string.IsNullOrEmpty(mobile) ? " " : " and IB_Send like '%" + mobile + "%' ");
            //sb.Append(string.IsNullOrEmpty(mobile) ? "" : " and ib_send in(select ct_mobile from cg_contacts where ct_name like '%" +
            //   mobile + "%')or ib_send in(select ib_send from cg_inbox where ib_send like '%" + mobile + "%')");
        }
        else
        {
            sb.Append(string.IsNullOrEmpty(content) ? " " : " and IB_SMContent like '%" + content + "%' ");
        }
        sb.Append(string.IsNullOrEmpty(starttime) ? " " : " and IB_SendTime >= '" + starttime + "' ");
        sb.Append(string.IsNullOrEmpty(endtime) ? " " : " and IB_SendTime <= '" + endtime + "' ");

        //回馈留言
        sb.Append(" and RT_Type_FK =5");

        return sb.ToString();
    }

    private string FillString()
    {
        //获取查询条件
        string sent = txtCondition.Value.Trim();
        string smcontent = txtCondition.Value.Trim();
        string startime = txtStartTime.Text.Trim();
        string endtime = txtEndTime.Text.Trim();

        string strCondition = AppendString(sent, smcontent, startime, endtime);

        return strCondition;
    }
    #endregion

    #region 详细事件
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Details")
        {
            Cisoft.Cisoft3G.Model.InboxMO MO = new Cisoft.Cisoft3G.Model.InboxMO();
            Cisoft.Cisoft3G.BLL.InboxBO BO = new Cisoft.Cisoft3G.BLL.InboxBO();
            MO = BO.GetModel(System.Convert.ToInt32(e.CommandArgument));
            if (MO != null)
            {
                Cisoft.Cisoft3G.BLL.SmsTypeBO SmsTypebll = new Cisoft.Cisoft3G.BLL.SmsTypeBO();
                Cisoft.Cisoft3G.Model.SmsTypeMO SmsTypemodel = new Cisoft.Cisoft3G.Model.SmsTypeMO();
                lblType.Text = "普通";
                SmsTypemodel = SmsTypebll.GetModel(int.Parse(MO.RT_Type_FK.ToString()));
                if (SmsTypemodel != null)
                {
                    lblType.Text = SmsTypemodel.ST_TypeName;
                }


                //lblType.Text = MO.ST_SmsType.Trim();
                //lblSend.Text = MO.IB_Send.Trim();
                lblSend.Text = string.Format("{0}({1})", bllContactsShow.GetName(MO.IB_Send.Trim()), MO.IB_Send.Trim());
                ViewState["Mobile"] = MO.IB_Send.Trim();
                lblRecive.Text = "系统";
                DateTime dt = Convert.ToDateTime(MO.IB_SendTime);
                lblSendTime.Text = dt.GetDateTimeFormats('f')[0].ToString();
                lblContent.Text = MO.IB_SMContent.Trim();
            }

            this.ModalPopupExtender3.Show();
        }
    }
    #endregion

    #region 显示详细信息
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = sender as LinkButton;
        this.ModalPopupExtender3.Show();
    }
    #endregion

    #region 导出筛选记录
    /// <summary>
    /// 导出筛选记录
    /// fxc 201003101115
    /// </summary>
    protected void btnExportFilter_Click(object sender, EventArgs e)
    {
        DataTable dt = CreateDateTable();

        int cpn = AspNetPager1.CurrentPageIndex;
        DataTable tempdt = new Cisoft.Cisoft3G.BLL.InboxBO().GetList(FillString() + " order by IB_SendTime desc").Tables[0];
        if (tempdt.Rows.Count > 0)
        {
            for (int i = 0; i < tempdt.Rows.Count; i++)
            {
                object[] obs = { (i + 1).ToString(), string.Format("{0}({1})",bllContactsShow.GetName(tempdt.Rows[i][2].ToString()),tempdt.Rows[i][2].ToString()), tempdt.Rows[i][3].ToString(), tempdt.Rows[i][4].ToString() };
                DataRow row = dt.NewRow();
                row.ItemArray = obs;
                dt.Rows.Add(row);
            }
        }

        gvData.DataSource = dt;
        gvData.DataBind();
        ShowExcel();
    }
    #endregion

    #region 导出本页记录
    /// <summary>
    /// 导出本页记录
    /// fxc 201003101205
    /// </summary>
    protected void btnExportThisPage_Click(object sender, EventArgs e)
    {
        DataTable dt = CreateDateTable();

        int cpn = AspNetPager1.CurrentPageIndex;
        DataTable tempdt = new Cisoft.Cisoft3G.BLL.InboxBO().GetListWithPage(cpn, 13, FillString()).Tables[0];
        if (tempdt.Rows.Count > 0)
        {
            for (int i = 0; i < tempdt.Rows.Count; i++)
            {
                object[] obs = new object[4] { (i + 1).ToString(), string.Format("{0}({1})",bllContactsShow.GetName(tempdt.Rows[i][2].ToString()),tempdt.Rows[i][2].ToString()), tempdt.Rows[i][3].ToString(), tempdt.Rows[i][4].ToString() };
                DataRow row = dt.NewRow();
                row.ItemArray = obs;
                dt.Rows.Add(row);
            }
        }

        gvData.DataSource = dt;
        gvData.DataBind();
        ShowExcel();
    }
    #endregion

    #region 自定义DataTable
    /// <summary>
    /// 自定义DataTable
    /// fxc 201003101209
    /// </summary>
    /// <returns>已经绑定了数据的DataTable</returns>
    private DataTable CreateDateTable()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("序号"));
        dt.Columns.Add(new DataColumn("回馈留言人"));
        dt.Columns.Add(new DataColumn("回馈留言内容"));
        dt.Columns.Add(new DataColumn("回馈留言时间"));

        return dt;
    }
    #endregion

    #region 导出Excel
    public void ShowExcel()
    {
        Response.Clear();
        Response.Buffer = false;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("回馈留言记录", System.Text.Encoding.UTF8) + ".xls");

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
        gvData.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    #endregion

    #region 选择删除提示
    //显示选择删除
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "false")
        {
            lblTS.Text = "<br /><h4>请选择要删除的记录</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = false;
            ModalPopupExtender1.Show();
        }
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "true")
        {
            lblTS.Text = "<br /><h4>你确认删除吗？</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = true;
            ModalPopupExtender1.Show();
        }
    }
    protected void btnConfirm2_Click(object sender, EventArgs e)
    {
        if (hihDelete.Value == "true")
        {
            Button1_Click(sender, e);
        }
        else
        {

        }
    }
    #endregion

    #region 数据绑定事件
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //姓名 类型
            Label lblSent = (Label)e.Item.FindControl("lblSend");
            lblSent.Text = bllContactsShow.GetName(DataBinder.Eval(e.Item.DataItem, "IB_Send").ToString());
            lblSent.Text += "(" + DataBinder.Eval(e.Item.DataItem, "IB_Send").ToString() + ")";
        }
    }
    #endregion

    protected void btnFeedBack_Click(object sender, EventArgs e)
    {
        //Response.Redirect("../AddSms.aspx?Mobile=" + lblSend.Text + "&SmsType=" + Server.HtmlEncode("回馈留言"));
        Response.Redirect("../AddSms.aspx?Mobile=" + ViewState["Mobile"].ToString() + "&SmsType=" + Server.HtmlEncode("回馈留言"));
    }

    #region GridView导出Excel显示数字
    protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }
    }
    #endregion
}
